#include "StdAfx.h"
#include "SimpleBinaryTree.h"

// http://math.hws.edu/eck/cs225/s03/binary_trees/
// http://www.cprogramming.com/tutorial/lesson18.html

// http://www.codingskills4u.com/2013/04/c-program-to-swap-kth-node-from-first.html
// http://www.codingskills4u.com/

namespace quiz
{

//===========================================================================
SimpleBinaryTree::SimpleBinaryTree( void )
    : m_root( NULL )
{
}

//===========================================================================
SimpleBinaryTree::~SimpleBinaryTree( void )
{
    this->CleanUp();
}

//===========================================================================
void SimpleBinaryTree::Insert(
    unsigned int item
) {
    return this->Insert( &this->m_root, item );
}

//===========================================================================
void SimpleBinaryTree::Insert(
    Node **      parent,
    unsigned int data
) {
    Node * node = *parent;
    if( !node ) {
        node = new Node( data );
        *parent = node;
        return;
    }

    if( node->data > data ) 
        this->Insert( &node->left,  data );
    else
        this->Insert( &node->right, data );
}

//===========================================================================
Node * SimpleBinaryTree::Search(
    unsigned int item
) {
    return this->Search(
        this->m_root,
        item
    );
}

//===========================================================================
Node * SimpleBinaryTree::Search(
    Node *       node,
    unsigned int data
) {
    if( node )
    {
        if( node->data == data )
            return node;

        if( node->data > data )
            return this->Search( node->left,  data );
        else
            return this->Search( node->right, data );
    }

    return NULL;
}

//===========================================================================
void SimpleBinaryTree::ReplaceNodeValueWithSumGreater( void )
{
    
    
    this->ReplaceNodeValueWithSumGreater( this->m_root );
}

//===========================================================================
void SimpleBinaryTree::ReplaceNodeValueWithSumGreater(
    Node * node
) {
    
}

//===========================================================================
void SimpleBinaryTree::Trim(
    unsigned int min,
    unsigned int max
) {
    this->m_root = this->Trim(
        this->m_root,
        max,
        min
    );
}

//===========================================================================
Node * SimpleBinaryTree::Trim(
    Node *       node,
    unsigned int min,
    unsigned int max
) {
    if( !node )
        return NULL;

    if( node->data > max ) return this->Trim( node->left,  min, max );
    if( node->data < min ) return this->Trim( node->right, min, max );

    node->left  = this->Trim( node->left,  min, max );
    node->right = this->Trim( node->right, min, max );
    
    return node;
}

//===========================================================================
void SimpleBinaryTree::CleanUp( void )
{
    if( this->m_root ) {
        delete this->m_root;
        this->m_root = NULL;
    }
}

};